package org.alex.samples.web;

import java.util.List;
import org.alex.samples.model.ExamSubject;
import org.alex.samples.service.IExamSubjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class ExamSubjectController {

	@Autowired
	private IExamSubjectService examSubjectService;
	
//	@Autowired
//	private LocaleResolver localeResolver;
	
	@Autowired
	private MessageSource messageSource;
	

	
	//查询全部方法
	@RequestMapping("/examSubjects")
	public String examSubjects(Model model){
		List<ExamSubject> list=examSubjectService.findAllExamSubject();
		model.addAttribute("edit", messageSource.getMessage("page.index.edit", null,LocaleContextHolder.getLocale()));
		model.addAttribute("examSubjectList", list);
		return "examSubject/examSubjectList";
	}

	
//新增方法
	@RequestMapping(value="/addExamSubject",method=RequestMethod.GET)
	public String toAdd(Model model){
		model.addAttribute("examSubject", new ExamSubject());
		return "examSubject/addExamSubject";
	}
	
	@RequestMapping(value="/addExamSubject" ,method=RequestMethod.POST)
	public String add(RedirectAttributes redirectAttributes,@Validated ExamSubject examSubject,BindingResult result){
		if(result.hasErrors()){
			return "examSubject/addExamSubject";
		}
		try{
			examSubjectService.createExamSubject(examSubject);
			redirectAttributes.addFlashAttribute("message", "添加成功。");
			
		}catch(Exception e){
			e.printStackTrace();
			return "error";
			
		}
		return "redirect:/examSubjects";
	}
	
	
	
	//修改方法
	@RequestMapping(value="/editExamSubject/{subId}", method=RequestMethod.GET)
	public String toEdit(Model model,@PathVariable Integer subId){
		ExamSubject examSubject=examSubjectService.getExamSubjectById(subId);
		model.addAttribute("examSubject",examSubject);
		return "examSubject/editExamSubject";
	}
	
	@RequestMapping(value="/editExamSubject/{subId}", method=RequestMethod.POST)
	public String edit(RedirectAttributes redirectAttributes,ExamSubject examSubject,@PathVariable Integer subId){
		try{
			
			examSubject.setSubId(subId);
			examSubjectService.editExamSubject(examSubject);
			redirectAttributes.addFlashAttribute("message", "修改成功.");
		}catch(Exception e){
			e.printStackTrace();
			return "error";
		}
		return "redirect:/examSubjects";
	}
	
	
	//测试删除方法  （添加一个dele.jsp,完善删除功能，提示是否删除）
	@RequestMapping(value="/deleExamSubject/{subId}",method=RequestMethod.GET)
	public String toDele(Model model,@PathVariable Integer subId){
		ExamSubject examSubject=examSubjectService.getExamSubjectById(subId);
		model.addAttribute("examSubject",examSubject);
		return "examSubject/deleExamSubject";
	}
	
	@RequestMapping(value="/deleExamSubject/{subId}",method=RequestMethod.POST)
	public String delete(RedirectAttributes redirectAttributes,@PathVariable Integer subId){
		try{
			examSubjectService.deleteExamSubject(subId);
//			redirectAttributes.addFlashAttribute("message", "删除成功.");
		}catch(Exception e){
			redirectAttributes.addFlashAttribute("message", "删除失败.");
			e.printStackTrace();
			return "error";
		}
		return "redirect:/examSubjects";
	}
		
	
}
